package tmproject.hlhj.fhp.anwser

import android.graphics.Color
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.bumptech.glide.Glide
import com.lzy.okgo.OkGo
import com.lzy.okgo.cookie.CookieJarImpl
import com.lzy.okgo.cookie.store.SPCookieStore
import com.lzy.okgo.interceptor.HttpLoggingInterceptor
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.aty_anwserhome.view.*
import okhttp3.OkHttpClient
import tmproject.hlhj.fhp.anwser.bean.*
import tmproject.hlhj.fhp.anwser.customview.AnwserDilaog
import tmproject.hlhj.fhp.anwser.customview.LoginDialog
import tmproject.hlhj.fhp.anwser.http.*
import tmproject.hlhj.fhp.anwser.utils.LoadingUtils
import tmproject.hlhj.fhp.anwser.utils.MyUtils
import tmproject.hlhj.fhp.anwser.utils.SPHelper
import java.util.logging.Level

/**
 * Created by Never Fear   on 2018\8\27 0027.
Never More....
 */
class BaseFgm :Fragment() {
    private lateinit var rootView:View
    private var nAid=0
    var loginDialog: LoginDialog?=null
    private var rid=0
    private var ntype=1
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView=inflater.inflate(R.layout.aty_anwserhome,null)

        initView()
        despatchData()
        setListener()




        return rootView

    }

    private fun setListener() {
        rootView.btStart.setOnClickListener {
            Apis.getActiveDetail(nAid, SPHelper.getToken(context!!)!!)
                    ?.let {
                        it.doOnSubscribe(LoadingAction(context!!))
                                .subscribe(object : BaseAction<Response<ActiveDetailBean>> {
                                    override fun httpSuccess(t: com.lzy.okgo.model.Response<ActiveDetailBean>) {
                                        if (t.body().data.has_answered==1){
                                            Toast.makeText(context,"您已经参与过此活动", Toast.LENGTH_SHORT).show()
                                        }else{
                                            if (Contents.Token.isEmpty()){
                                                loginDialog?.show()
                                            }else{
                                                AnwserDilaog(context!!,nAid).show()
                                            }
                                        }
                                    }

                                    override fun httpErroCode(baseBean: BaseBean) {
                                        if (baseBean.code==500){
                                            loginDialog?.show()
                                        }
                                    }
                                    override fun httpFail(e: Throwable?) {
                                        LoadingUtils.loadingDismiss()
                                        loginDialog?.show()
                                    }
                                })
                    }
        }
        rootView.btNow.setOnClickListener {
            getActive(1)
            ntype=1
            rootView.btStart.setBackgroundResource(R.drawable.start_border)
            rootView.btStart.isClickable=true
            rootView.btNow.setTextColor(Color.WHITE)
            rootView.btNext.setTextColor(Color.parseColor("#333333"))
            rootView. btNow.setBackgroundResource(R.drawable.select_border)
            rootView.btNext.setBackgroundResource(R.drawable.unselect_border)
        }
        rootView. btNext.setOnClickListener {
            getActive(2)
            ntype=2
            rootView.btStart.isClickable=false
            rootView. btStart.setBackgroundResource(R.drawable.start_border_disable)
            rootView.btNext.setTextColor(Color.WHITE)
            rootView. btNow.setTextColor(Color.parseColor("#333333"))
            rootView. btNext.setBackgroundResource(R.drawable.select_border)
            rootView.btNow.setBackgroundResource(R.drawable.unselect_border)
        }

        rootView.btActive.setOnClickListener {
            hideRule()
        }
        rootView.btRule.setOnClickListener {
            getRule(rid)
        }
    }

    private fun initView() {
        Contents.Token=SPHelper.getToken(context!!)!!

        val builder = OkHttpClient.Builder()
        builder.cookieJar(CookieJarImpl(SPCookieStore(context)))

        loginDialog=LoginDialog(context!!)
        loginDialog?.listener=object :LoginDialog.LoginListener{
            override fun loginSuccess() {
                despatchData()
            }
        }
        /*Log*/
        val loggingInterceptor = HttpLoggingInterceptor("okgo")
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY)
        loggingInterceptor.setColorLevel(Level.INFO)
        builder.addInterceptor(loggingInterceptor)
        OkGo.getInstance().okHttpClient = builder.build()

    }

    private fun despatchData() {
        getPrize()

        getActive(1)
        /*用户信息*/
        Apis.getUserInfo(SPHelper.getToken(context!!)!!)
                ?.let {
                    it.doOnSubscribe(LoadingAction(context!!))
                            .subscribe(object :BaseAction<com.lzy.okgo.model.Response<UserInfoBean>>{
                                override fun httpSuccess(t: com.lzy.okgo.model.Response<UserInfoBean>) {
                                    val userInfoBean = t.body()
                                    rootView.myPoint.text=userInfoBean.data.points.toString()
                                    rootView.myTimes.text=userInfoBean.data.drawable_times.toString()
                                }

                                override fun httpErroCode(t:BaseBean) {
                                    rootView.myPoint.text="登录查看"
                                    rootView.myTimes.text="登录查看"
                                    rootView.myTimes.setOnClickListener {
                                        loginDialog?.show()
                                    }
                                    rootView.myPoint.setOnClickListener {
                                        loginDialog?.show()

                                    }
                                }
                                override fun onCompleted() {
                                    LoadingUtils.loadingDismiss()
                                    super.onCompleted()
                                }
                                override fun httpFail(e: Throwable?) {
                                    rootView.myPoint.text="登录查看"
                                    rootView.myTimes.text="登录查看"
                                    rootView.myTimes.setOnClickListener {
                                        loginDialog?.show()
                                    }
                                    rootView.myPoint.setOnClickListener {
                                        loginDialog?.show()

                                    }
                                }
                            })
                }
    }

    //1-当前活动 2-下棋活动
    private fun getActive(type:Int) {
        Apis.getActiveList(type)
                ?.let {
                    it.doOnSubscribe(LoadingAction(context!!))
                            .subscribe(object : BaseAction<com.lzy.okgo.model.Response<ActiveBean>> {
                                override fun httpSuccess(t: com.lzy.okgo.model.Response<ActiveBean>) {
                                    rootView.endTime.visibility=View.VISIBLE

                                    rootView.lo_x.visibility=View.VISIBLE
                                    rootView.dv.visibility=View.VISIBLE
                                    rootView.endTime.text= "${MyUtils.timeStampToStrYMD(t.body().data.end_time.toLong())}截止"
                                    rootView.prize.text  = "${t.body().data.title}"
                                    if (type==1){
                                        nAid=t.body().data.id
                                    }
                                    rid=t.body().data.id
                                }
                                override fun httpErroCode(t: BaseBean) {
                                    rootView.endTime.visibility=View.VISIBLE
                                    rootView.lo_x.visibility=View.GONE
                                    rootView.dv.visibility=View.GONE
                                    if (type==1){
                                        rootView.endTime.text="活动已结束"
                                        rootView.btStart.isClickable=false
                                        rootView.btStart.setBackgroundResource(R.drawable.start_border_disable)
                                    }else{
                                        rootView.endTime.text="暂无计划"
                                    }
                                }
                                override fun httpFail(e: Throwable?) {
                                    LoadingUtils.loadingDismiss()
                                    rootView.endTime.visibility=View.VISIBLE
                                    rootView.lo_x.visibility=View.GONE
                                    rootView.dv.visibility=View.GONE
                                    if (type==1){
                                        rootView.endTime.text="活动已结束"
                                        rootView.btStart.isClickable=false
                                        rootView.btStart.setBackgroundResource(R.drawable.start_border_disable)
                                    }else{
                                        rootView.endTime.text="暂无计划"
                                    }
                                }
                            })
                }
    }
    /*规则*/
    private fun getRule(type:Int){
        Apis.getActiveList(ntype)
                ?.let {
                    it.doOnSubscribe(LoadingAction(context!!))
                            .subscribe(object : BaseAction<com.lzy.okgo.model.Response<ActiveBean>>{
                                override fun httpSuccess(t: com.lzy.okgo.model.Response<ActiveBean>) {
                                    showRule()
                                    rootView.tvObject.text=t.body().data.`object`
                                    rootView.tvDetail.text=t.body().data.rule
                                }

                                override fun httpErroCode(baseBean: BaseBean) {
                                }

                                override fun httpFail(e: Throwable?) {
                                }
                            })
                }
    }

    private fun getPrize(){
        Apis.getPrize()
                ?.let {
                    it
                            .doOnSubscribe{
                            }
                            .subscribe(object :BaseAction<com.lzy.okgo.model.Response<PrizeBean>>{
                                override fun httpSuccess(t: com.lzy.okgo.model.Response<PrizeBean>) {
                                    val prizeBean = t.body()
                                    Glide.with(context!!).load(Urls.BaseUrl+prizeBean.data[0].pic)
                                            .apply(GlideTools.LoadOption())
                                            .into(rootView.prizeIcon)
                                }

                                override fun httpErroCode(baseBean: BaseBean) {
                                    LoadingUtils.loadingDismiss()
                                }

                                override fun httpFail(e: Throwable?) {
                                    LoadingUtils.loadingDismiss()
                                }
                            })
                }
    }
    fun refresh(){
        despatchData()
    }
    private fun showRule(){
        rootView.ruleObject.visibility=View.VISIBLE
        rootView.ruleDetail.visibility=View.VISIBLE
        rootView.lo_1.visibility=View.VISIBLE
        rootView.lo_2.visibility=View.VISIBLE
        rootView.btStart.visibility=View.GONE
        rootView.loInfo.visibility=View.GONE
        rootView.btActive.visibility=View.VISIBLE
        rootView.btRule.visibility=View.GONE
    }
    private fun hideRule(){
        rootView.ruleObject.visibility=View.GONE
        rootView.ruleDetail.visibility=View.GONE
        rootView.lo_1.visibility=View.GONE
        rootView.lo_2.visibility=View.GONE
        rootView.btStart.visibility=View.VISIBLE
        rootView.loInfo.visibility=View.VISIBLE
        rootView.btActive.visibility=View.GONE
        rootView.btRule.visibility=View.VISIBLE
    }
}